<template>
  <el-main>
    <!-- 搜索栏 -->
    <el-form :model="listParm" label-width="80px" :inline="true" size="small">
      <el-form-item>
        <el-input
          placeholder="请输入角色名称"
          v-model="listParm.name"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button :icon="Search" @click="searchBtn">搜索</el-button>
        <el-button :icon="Close" style="color: #FF7670;" @click="resetBtn"
          >重置</el-button
        >
        <el-button
          type="primary"
          v-permission="['sys:role:add']"
          :icon="Plus"
          @click="addBtn"
          >新增</el-button
        >
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table :height="tableHeight" :data="roleTable.list" border stripe>
      <el-table-column prop="name" label="角色名称"></el-table-column>
      <el-table-column prop="remark" label="角色备注"></el-table-column>
      <el-table-column label="操作" align="center" width="300">
        <template #default="scope">
          <el-button
            type="primary"
            size="small"
            :icon="Edit"
            @click="editBtn(scope.row)"
            v-permission="['sys:role:edit']"
            >编辑</el-button
          >
          <el-button
            type="primary"
            size="small"
            :icon="Setting"
            v-permission="['sys:role:assign']"
            @click="assignPermission(scope.row.id, scope.row.name)"
            >分配权限</el-button
          >
          <el-button
            type="danger"
            size="small"
            v-permission="['sys:role:delete']"
            @click="deleteBtn(scope.row.id)"
            :icon="Delete"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="sizeChange"
      @current-change="currentChange"
      :current-page.sync="listParm.currentPage"
      :page-sizes="[1, 10, 20, 40, 80, 100]"
      :page-size="listParm.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="listParm.total"
      background
    ></el-pagination>
  </el-main>
  <!-- 新增编辑对话框 -->
  <AddRoleVue ref="addRoleRef" @save="save"></AddRoleVue>
  <!-- 分配权限对话框 -->
  <AssignMenuVue ref="assignMenuRef"></AssignMenuVue>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted, nextTick } from 'vue'
import { Search, Close, Plus, Delete, Edit, Setting } from '@element-plus/icons-vue'
// 导入表格的操作方式，页码改变，查询数据
import useRoleTable from '@/composables/role/useRoleTable'
// 导入对话框增删改查的方式
import useRole from '@/composables/role/useRole'
// 导入对话框组件
import AddRoleVue from './AddRole.vue'
import AssignMenuVue from './AssignMenu.vue'
// 表格高度
const tableHeight = ref(0)
const {
  roleTable,
  listParm,
  getRoleList,
  searchBtn,
  resetBtn,
  sizeChange,
  currentChange
} = useRoleTable()
// 把对话框的操作方式引进来
const {
  addBtn,
  editBtn,
  deleteBtn,
  assignPermission,
  addRoleRef,
  save,
  assignMenuRef
} = useRole(getRoleList)
onMounted(() => {
  nextTick(() => {
    tableHeight.value = window.innerHeight - 220
  })
})
</script>
<style scoped lang="scss"></style>
